import request from "../../utils/request"
const appInstance = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isPlay: false,
        song: {},
        musicId: ''
    },

    handleMusicPlay(event) {
        // console.log(event);
        this.setData({
            isPlay: !this.data.isPlay
        })
        this.musicControl(this.data.isPlay, this.data.musicId)

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // console.log(options.musicId);
        let musicId = options.musicId
        this.setData({
            musicId
        })

        if (appInstance.globalData.isMusicPlay && appInstance.globalData.musicId === musicId) {
            this.setData({
                isPlay: true
            })
        }

        this.backgroundAudioManager = wx.getBackgroundAudioManager()
        this.getMusicInfo(musicId)
        this.backgroundAudioManager.onPlay(() => {
            this.changePlayState(true)
            appInstance.globalData.musicId = musicId
        })
        this.backgroundAudioManager.onPause(() => {
            // console.log('pause()');
            this.changePlayState(false)
        })
        this.backgroundAudioManager.onStop(() => {
            // console.log('pause()');
            this.changePlayState(false)
        })
    },
    // 修改
    changePlayState(isPlay) {
        this.setData({
            isPlay
        })
        appInstance.globalData.isMusicPlay = isPlay
    },
    async getMusicInfo(musicId) {
        const songData = await request('/song/detail', {
            ids: musicId
        })
        this.setData({
            song: songData.songs[0]
        })
        wx.setNavigationBarTitle({
            title: this.data.song.name
        })
    },
    async musicControl(isPlay, musicId) {

        if (isPlay) {
            const musicLinkData = await request('/song/url', { id: musicId })
            const musicLink = musicLinkData.data[0].url
            this.backgroundAudioManager.src = musicLink
            this.backgroundAudioManager.title = this.data.song.name
        } else {
            this.backgroundAudioManager.pause()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})